﻿Imports System.Data.SqlClient

Public Class frmProductAct

    Public Sub bindtypetem()
        cbtypeid.Items.Clear()
        cbtypeid.DisplayMember = "TypeName"
        cbtypeid.ValueMember = "TypeId"
        cbtypeid.DataSource = dbconnection.ExecuteDatatable("select Distinct TypeId, TypeName from Type where Active=1")
        cbtypeid.SelectedIndex = -1
    End Sub
    Public Sub bindcatetem(ByVal typeid As String)
        cbcateid.DisplayMember = "CategoryName"
        cbcateid.ValueMember = "CategoryId"
        Dim condi As String
        condi = "and TypeId='" + typeid + "'"
        cbcateid.DataSource = dbconnection.ExecuteDatatable("select CategoryId, CategoryName from Category where Active=1 " + condi + ";")
        cbcateid.SelectedIndex = -1
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Close()
            With frmProduct
                .bindData("")
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmProduct)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString())
        End Try
    End Sub

    Private Sub cbtypeid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbtypeid.SelectedIndexChanged
        Try
            Me.bindcatetem(cbtypeid.SelectedValue)
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
        End Try
    End Sub
    Private Shared Function validprocode(ByVal procode As String, ByVal proid As String)
        Try
            Dim pchave = dbconnection.ExecuteScalar("select * from Product where ProductCode=N'" + procode + "' and ProductId<>'" + proid + "';")
            Return pchave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function
    Private Shared Function validproname(ByVal proname As String, ByVal cateid As String, ByVal proid As String)
        Try
            Dim pnhave = dbconnection.ExecuteScalar("select * from Product where ProductName=N'" + proname + "' and ProductId<>'" + proid + "' and CategoryId='" + cateid + "';")
            Return pnhave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If (txtProductCode.Text = "" Or txtProductName.Text = "" Or txtPrice.Text = "" Or
                txtMinimumPrice.Text = "" Or cbcateid.SelectedIndex = -1) Then
                MessageBox.Show("Errors: All Field Require...")
            Else
                If (validprocode(txtProductCode.Text.Trim, lblProductId.Text) IsNot Nothing) Then
                    MessageBox.Show("Errors: Product Code Already Exists Please Try With Other...!")
                    txtProductCode.Focus()
                Else
                    If (validproname(txtProductName.Text.Trim, cbcateid.SelectedValue, lblProductId.Text) IsNot Nothing) Then
                        MessageBox.Show("Errors: Product Name of Category Already Exists Please Try With Other...!")
                        txtProductName.Focus()
                    Else
                        If (CheckValid.IsDecimal(txtPrice.Text.Trim) = False) Then
                            MessageBox.Show("Price decimal number only")
                            txtPrice.Focus()
                            Exit Sub
                        End If
                        If (CheckValid.IsDecimal(txtMinimumPrice.Text.Trim) = False) Then
                            MessageBox.Show("Minimum Price decimal number only")
                            txtMinimumPrice.Focus()
                            Exit Sub
                        End If
                        If (lblProductId.Text = "") Then
                            Try
                                Dim cmdadd As SqlCommand
                                cmdadd = dbconnection.Connection.CreateCommand
                                With cmdadd
                                    .CommandType = CommandType.Text
                                    .CommandText = "insert into Product (ProductCode, ProductName, CategoryId, Price, MinimumPrice, Active) " & _
                                    "values (@procode, @proname, @cateid, @price, @minimumprice, '1');"
                                    .Parameters.Add("@procode", SqlDbType.NVarChar).Value = txtProductCode.Text.Trim()
                                    .Parameters.Add("@proname", SqlDbType.NVarChar).Value = txtProductName.Text.Trim()
                                    .Parameters.Add("@cateid", SqlDbType.NVarChar).Value = cbcateid.SelectedValue
                                    .Parameters.Add("@price", SqlDbType.NVarChar).Value = txtPrice.Text.Trim()
                                    .Parameters.Add("@minimumprice", SqlDbType.NVarChar).Value = txtMinimumPrice.Text.Trim()
                                    .ExecuteNonQuery()
                                End With
                                MessageBox.Show("Record Added")
                                Me.Close()
                                With frmProduct
                                    .bindData("")
                                    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                                    .TopLevel = False
                                    .Dock = DockStyle.Fill
                                    frmMain.pnBody.Controls.Add(frmProduct)
                                    .Show()
                                End With
                            Catch ex As Exception
                                MessageBox.Show("Errors " + ex.ToString)
                            End Try
                        Else
                            Try
                                Dim cmdupdate As SqlCommand
                                cmdupdate = dbconnection.Connection.CreateCommand
                                With cmdupdate
                                    .CommandType = CommandType.Text
                                    .CommandText = "update Product set ProductCode=@procode, ProductName=@proname, CategoryId=@cateid, " & _
                                    " Price=@price, MinimumPrice=@minimumprice where ProductId=@proid;"
                                    .Parameters.Add("@proid", SqlDbType.NVarChar).Value = lblProductId.Text.Trim()
                                    .Parameters.Add("@procode", SqlDbType.NVarChar).Value = txtProductCode.Text.Trim()
                                    .Parameters.Add("@proname", SqlDbType.NVarChar).Value = txtProductName.Text.Trim()
                                    .Parameters.Add("@cateid", SqlDbType.NVarChar).Value = cbcateid.SelectedValue
                                    .Parameters.Add("@price", SqlDbType.NVarChar).Value = txtPrice.Text.Trim()
                                    .Parameters.Add("@minimumprice", SqlDbType.NVarChar).Value = txtMinimumPrice.Text.Trim()
                                    .ExecuteNonQuery()
                                End With
                                MessageBox.Show("Record Update")
                                Me.Close()
                                With frmProduct
                                    .bindData("")
                                    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                                    .TopLevel = False
                                    .Dock = DockStyle.Fill
                                    frmMain.pnBody.Controls.Add(frmProduct)
                                    .Show()
                                End With
                            Catch ex As Exception
                                MessageBox.Show("Errors " + ex.ToString)
                            End Try
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
        End Try
    End Sub
End Class